package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kindergarten.pojo.Page;
import com.kindergarten.pojo.Pay;
import com.kindergarten.service.IPageService;
import com.kindergarten.service.IPayService;
import com.kindergarten.service.IRoleService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2022-10-28
 */
@Controller
@RequestMapping("/page-api")
public class PageController {

    @Autowired
    IPageService pageService;

    @Autowired
    IRoleService roleService;


    /**
     * 角色管理页面的树形所有菜单
     * @return
     */
    @ResponseBody
    @PostMapping("getAllPage")
    public List<Page> getAllPage(){
        // 查询所有菜单
        return pageService.list(new QueryWrapper<Page>().eq("delete_flag", 0).eq("type",0));
    }

    /**
     * 角色管理页面的树形要回显的菜单
     * @return
     */
    @ResponseBody
    @PostMapping("getPageByRole")
    public List<Page> getPageByRole(Integer roleId){
        return pageService.findMyMenus(roleId.toString());
    }


    @RequestMapping("topage")
    public String topage(Model model){
        // 查询所有菜单
        List<Page> pageList = pageService.list(new QueryWrapper<Page>().eq("delete_flag", 0).eq("type",0));
        model.addAttribute("pageList",pageList);
        return "page";
    }

    /**
     * add
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    public DataResults delete(Page page){
        pageService.save(page);
        Integer pageId = page.getPageId();
        return DataResults.success(ResultCode.SUCCESS,pageId);
    }

    /**
     * update
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public DataResults update(Page page){
        pageService.updateById(page);
        Integer pageId = page.getPageId();
        return DataResults.success(ResultCode.SUCCESS,pageId);
    }

    /**
     * 删除
     * @return
     */
    @ResponseBody
    @DeleteMapping("delete")
    public DataResults update(Integer id){
        Page page=new Page();
        page.setPageId(id);
        page.setDeleteFlag(1);
        return DataResults.success(ResultCode.SUCCESS,pageService.updateById(page));
    }
}
